#!/usr/bin/env python
# -*- coding:utf-8 -*-

import sys

from PySide import QtCore, QtGui, QtNetwork

from qui.login import *

class Login(QtGui.QDialog):
    '''
    Longin
    '''
    def __init__(self, parent=None, auth2 = None):
        super(Login, self).__init__(parent)
        self.ui = Ui_Dialog()
        self.ui.setupUi(self)
        
        self.ui.webViewLogin.page().networkAccessManager().finished.connect(self._onFinished)
        self.ui.webViewLogin.urlChanged.connect(self._urlPrint)
        
        self.auth2 = auth2
        
        #def getLoginInterface(self, url):
        self.count = 0
        #self.request = QtNetwork.QNetworkRequest(url)
        self.request = QtNetwork.QNetworkRequest(self.auth2.getAuth2Uri())
        self.ui.webViewLogin.load(self.request)

        
    def _urlPrint(self, url):
        print url
        
    def _onFinished(self, reply):
        self.count += 1
        print self.count
        #print reply.readBufferSize ()
        #print reply.rawHeaderList()
        #print reply.request()
        qUrl = reply.request().url()
        print qUrl
        if self.auth2.getAuthCodeFromUri(qUrl.toString()) != None:
            self.ui.buttonBox.accepted.emit()
        #print reply.rawHeaderPairs()

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    win = Login()
    win.show()
    sys.exit(app.exec_())  
